using System;
using System.Collections.Generic;
using System.Text;

namespace eJack.Utility
{
	public class CxInt
	{
		//-------------------------------------------------------------------------
		/// <summary>
		/// Converts string to integer. Returns defValue if conversion failed.
		/// </summary>
		/// <param name="s">string to parse as integer</param>
		/// <param name="defValue">value to return if conversion failed</param>
		/// <returns>parse value or default value if conversion failed</returns>
		static public int Parse(string s, int defValue)
		{
			if (CxUtils.NotEmpty(s))
			{
				int result;
				if (int.TryParse(s.Trim(), out result))
				{
					return result;
				}
			}
			return defValue;
		}
		//-------------------------------------------------------------------------
		/// <summary>
		/// Converts object to integer. Returns defValue if conversion failed.
		/// </summary>
		/// <param name="o">object to parse as integer</param>
		/// <param name="defValue">value to return if conversion failed</param>
		/// <returns>parse value or default value if conversion failed</returns>
		static public int Parse(object o, int defValue)
		{
			if (CxUtils.NotEmpty(o))
			{
				if (o is string)
				{
					return Parse((string)o, defValue);
				}
				try
				{
					return Convert.ToInt32(o);
				}
				catch
				{
					return defValue;
				}
			}
			return defValue;
		}
		//-------------------------------------------------------------------------
	}
}